package com.caine.multithread;

/**
 * @Author: CaineZhu
 * @Description: cpu存在乱序执行
 * @Date: Created in 15:05 2020/12/10
 * @Modified By:
 */
public class Demo06 {
    static int a = 0, b = 0;
    static int x = 0, y = 0;

    public static void main(String[] args) throws InterruptedException {

        long beginTime = System.currentTimeMillis();
        int i = 1;
        while (true) {
            i++;
            x = 0;
            y = 0;
            a = 0;
            b = 0;
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    a = 1;
                    x = b;
                }
            });

            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    b = 1;
                    y = a;
                }
            });
            t1.start();
            t2.start();
            t1.join();
            t2.join();
            if (x == 0 && y == 0) {
                System.out.println("第" + i + "次 x=" + x + " y=" + y + " 耗时:" + (System.currentTimeMillis() - beginTime));
                break;
            }
        }

    }
}
